/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer;

import java.util.HashMap;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;

import android.view.Menu;
import android.view.MenuItem;

import pcgen.android.app.AppUtils;
import pcgen.android.Logger;

import pcgen.CharacterViewer.fragments.AppTabContentFragment;
import pcgen.CharacterViewer.fragments.CharacterTabContentFragment;
import pcgen.CharacterViewer.fragments.WebTabContentFragment;
import pcgen.CharacterViewer.resources.GameResource;
import pcgen.CharacterViewer.resources.GameResourceCharacter;

public class ResourceWidgetPrevious implements IResourceWidget
{
	public ResourceWidgetPrevious(int contentId)
	{
		_contentId = contentId;
		_menuGroup = (new Random()).nextInt(10000000);
	}
	
	public void add(AppTabContentFragment fragment) 
	{
		if (_activity.getOptionMenu() == null)
			return;
		
		addEx(fragment);
    }
	
	public void addResourceCharacter(GameResource resource)
	{
		if (_activity.getOptionMenu() == null)
			return;
		
		if (!(resource instanceof GameResourceCharacter))
			return;
		
		CharacterTabContentFragment fragment = new CharacterTabContentFragment((GameResourceCharacter)resource);
		fragment.updateUrl(resource.getLocation());
		MenuItem item = addEx(fragment);
		_resources.put(resource, item);
	}
	
	public void addResource(GameResource resource)
	{
		if (_activity.getOptionMenu() == null)
			return;
		
		WebTabContentFragment fragment = new WebTabContentFragment(resource);
		fragment.updateUrl(resource.getLocation());
		MenuItem item = addEx(fragment);
		_resources.put(resource, item);
	}
	
	public boolean handleOptionsItemSelected(MenuItem item)
	{
		//if (item.getItemId() == R.id.menu_game_close)
		//{
			//gameFolderCloseConfirm();
			//return true;
		//}
		
		return false;
	}

	public void initialize(CharacterViewerActivity activity)
	{
		_activity = activity;	
	}
	
	public void removeCurrent() 
    {
    }

	public void removeResource(GameResource resource) 
    {
		if (_activity.getOptionMenu() == null)
			return;
		
		if (!_resources.containsKey(resource))
			return;
		
		MenuItem item = _resources.get(resource);
        for (int i = 0; i < _activity.getOptionMenu().size(); i++)
        {
        	MenuItem temp = _activity.getOptionMenu().getItem(i);
        	if (temp == item)
        		_activity.getOptionMenu().removeItem(item.getItemId());
        }
    }

	public void toggle() 
    {
    }

	public void toggleOff() 
    {
    }

	public void toggleOn() 
    {
    }

	public void removeAll() 
	{
		if (_activity.getOptionMenu() == null)
			return;
		
		_activity.getOptionMenu().removeGroup(_menuGroup);
    }
	
	private MenuItem addEx(AppTabContentFragment fragment) 
	{
		Menu menu = _activity.getOptionMenu();
		MenuItem item = menu.add(_menuGroup, 1000, 1, fragment.getName());
		return item;
    }
	
	private CharacterViewerActivity _activity;
	private int _contentId;
	private int _menuGroup = 0;
	private HashMap<GameResource, MenuItem> _resources = new HashMap<GameResource, MenuItem>();
}